<?php
if(isset($_POST['txtFName']))
{
	$fName = $_POST['txtFName'];
	$lName = $_POST['txtLName']; 
	$email =  $_POST['txtEmail'];
	$pwd =  $_POST['txtPwd'];	
	$lastLogin = date ("Y-m-d H:i:s");
	$uniqueFileName = date ("Y_m_d_H_i_s");

	require_once 'database.php';
	
	$sql = "SELECT * FROM forumuser WHERE email = '$email' AND isactive = 1";
	$result = mysql_query($sql);
	$count = mysql_num_rows($result);
	$randCode = md5(rand(0,999));
	$randCode = substr($randCode,15,5);
	if($count > 0)
	{
		$exist = 1;
	}
	else{	
	$sql = "INSERT INTO forumuser (fname,lname,email,pwd,lastlogin,isactive,ActivationCode)
			VALUES
			('$fName','$lName','$email','$pwd','$lastLogin',0,'$randCode')";

	mysql_query($sql);
	}
	
	$sql = "SELECT Id, ActivationCode FROM forumuser WHERE IsActive = 1 ORDER BY Id DESC LIMIT 1";
	$result = mysql_query($sql);
	$recordSet = mysql_fetch_array($result);
	$id = $recordSet['Id'];
	
	$clientImgPath = basename( $_FILES['userImg']['name']);

	if($clientImgPath != '')
	{
		require_once 'MyImageClass.php';
		$UploadImage = new imgUpload();
	
		$imgPath = $UploadImage->createImage('userImages/Actual/','userImages/Thumb/',$clientImgPath,$_FILES['userImg']['name'], $_FILES['userImg']['tmp_name'],$_FILES['userImg']['size'],$_FILES['userImg']['type'],$id);
	}else{
	$imgPath = "userImages/User.png";
	}
	
	$sql = 'UPDATE forumuser SET ImgPath = "'.$imgPath.'" WHERE id = '.$id;	
	
	//Send email code
	$headers = "MIME-Version: 1.0" . "\r\n";
	$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
	// More headers
	$headers .= 'From: <admin@shivasoft.in>' . "\r\n";
	$headers .= 'Bcc: ilovenagpur@gmail.com' . "\r\n";
	$to = $email;
	$subject = "Activation code";
	$message ="<a href='http://www.shivasoft.in/ActivateUser.php?id=".$id."&code=".$randCode."'>Click to activate your account</a>";

	/*
	if(mail($to,$subject,$message,$headers))
	{
		echo "<h1>Email sent!!!</h1>";
	}
	else
	{
		echo "Sending email failed!!!";
	}
*/
	echo $message;
	mysql_query($sql);
	mysql_close();
}
?>
<html>
<head>
<title> Template </title>
<?php
require_once 'headerFiles.html';
?>
<script type="text/javascript" src="script/jquery.validate.min.js"> </script>
</head>
<body>
	<div id="mainContainer">
		<?php
		require_once 'Header.php';		
		?>
		<div id="content">
		<br /><br />
			<form enctype="multipart/form-data" method="post" action="Register.php" id="registerForm">
			<table id="tblCenter">
				<tr>
					<td>First Name :</td>
					<td> <input class="required" type="text" name="txtFName" /></td>
				</tr>
				<tr>
					<td>Last Name :</td>
					<td> <input class="required" type="text" name="txtLName" /></td>
				</tr>
				<tr>
					<td>Email :</td>
					<td> <input class="required email" type="text" name="txtEmail" /></td>
				</tr>
				<tr>
					<td>password :</td>
					<td> <input class="required" type="text" name="txtPwd" /></td>
				</tr>
				<tr>
				<td>Select Picture :</td>
				<td>
				<input type="file" id="userImg" name="userImg" />
				</td>
				</tr>
				<tr>
				<td>
				Enter Code:
				</td>
				<td>
				<input type="text" name="code" size="8" />
				<img src="Captcha.php" />
				</td>
				</tr>
				<tr>
				<td colspan="2">
				<input type="submit" />
				</td>
				</tr>
				
			</table>			
			</form>
		</div>
		<?php
		require_once 'footer.html';		
		?>
	</div>
	<?php
	if(isset($_POST['txtFName']))
	{
		if($exist == 1)
		{
		?>
		<script type="text/javascript" >
		alert('Record already exist with same email id');
		</script>
		<?php
		}
		else
		{
		?>
		<script type="text/javascript" >
		alert('Record added succesfully');
		</script>
		<?php
		}
	}
	?>
	<script type="text/javascript">
	$("#registerForm").validate();
	</script>
</body>
</html>